package org.xshop.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import org.xshop.mapper.TMemberFootmarkMapper;
import org.xshop.model.TGoods;
import org.xshop.model.TGoodsPromotion;
import org.xshop.model.TMemberFootmark;
import org.xshop.service.ITGoodsService;
import org.xshop.service.ITMemberFootmarkService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;
import top.ibase4j.core.support.Pagination;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.DateUtil;
import top.ibase4j.core.util.InstanceUtil;

/**
 * <p>
 * 我的足迹 服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-05-21
 */
@MotanService(interfaceClass = ITMemberFootmarkService.class)
@CacheConfig(cacheNames = "TMemberFootmark")
public class MemberFootmarkService extends BaseServiceImpl<TMemberFootmark, TMemberFootmarkMapper>
implements ITMemberFootmarkService {

    @Autowired
    private ITGoodsService goodsService;

    @Override
    @Transactional
    public Object delAll(TMemberFootmark param) {
        if (DataUtil.isNotEmpty(param.getGoodsIds())) {
            String[] ids = param.getGoodsIds().split(",");
            TMemberFootmark footmark = new TMemberFootmark();
            footmark.setCreateBy(param.getCreateBy());
            for (String string : ids) {
                footmark.setGoodsId(Long.parseLong(string));
                deleteByEntity(footmark);
            }
        }
        return null;
    }

    @Override
    public Object queryAll(Map<String, Object> param) {
        Object pageNum = param.get("pageNum");
        if (pageNum != null) {
            param.put("pageIndex", pageNum);
        }
        Pagination<TMemberFootmark> page = super.query(param);
        JSONArray shopArr = new JSONArray();
        if (page != null) {
            List<TMemberFootmark> footmarks = page.getRecords();
            // 商店归属
            Map<Long, List<TGoods>> map = InstanceUtil.newLinkedHashMap();
            for (int i = 0; i < footmarks.size(); i++) {
                TMemberFootmark footmark = footmarks.get(i);
                TGoods goods = goodsService.queryById(footmark.getGoodsId());
                if (goods != null) {
                    if (goods.getIsPromotion() == 1) {
                        TGoodsPromotion promotion = goods.getPromotion();
                        if (promotion != null) {
                            Date now = DateUtil.stringToDate(DateUtil.getDate());
                            if (!promotion.getStartTime().after(now) && !promotion.getEndTime().before(now)) {
                                goods.setSalePrice(promotion.getPrice());
                            }
                        }
                    }
                    Long id = goods.getStore().getId();
                    if (map.containsKey(id)) {
                        map.get(id).add(goods);
                    } else {
                        List<TGoods> list = InstanceUtil.newArrayList();
                        list.add(goods);
                        map.put(id, list);
                    }
                } else {
                    deleteByEntity(footmark);
                }
            }
            Set<Long> set = map.keySet();
            Iterator<Long> iterator = set.iterator();
            while (iterator.hasNext()) {
                Long id = iterator.next();
                List<TGoods> goods = map.get(id);
                JSONObject goodsObj = (JSONObject)JSON.toJSON(goods.get(0).getStore());
                for (TGoods tGoods : goods) {
                    tGoods.setStore(null);
                }
                goodsObj.put("goods", goods);
                shopArr.add(goodsObj);
            }
        }
        return shopArr;
    }
}
